'''
Desc: 创建僵尸对象
Author: 陈爱国
Date: 2024-08-24 10:49:49
'''
import pygame
import random

class Zombie(pygame.sprite.Sprite):
    def __init__(self):
        # 调用父类构造方法
        super(Zombie, self).__init__()
        # 图片
        self.image = pygame.image.load('material/images/Zombie_0.png').convert_alpha()
        # 图片序列，用于产生动画
        self.images = [pygame.image.load('material/images/Zombie_{}.png'.format(i)).convert_alpha() for i in range(22)]
        # 死亡图片序列，用于产生动画
        self.dieimages = [pygame.image.load('material/images/ZombieDie_{}.png'.format(i)).convert_alpha() for i in range(10)]
        # 吃坚果图片序列，用于产生动画
        self.attackimages = [pygame.image.load('material/images/ZombieAttack_{}.png'.format(i)).convert_alpha() for i in range(21)]
        # 图片的矩形区域
        self.rect = self.images[0].get_rect()
        # 图片top
        self.rect.top = 25 + random.randrange(0, 4) * 125
        # 图片left
        self.rect.left = 1000
        # 速度
        self.speed = 5
        # 能量
        self.energy = 6
        # 死亡
        self.dietimes = 0
        # 是否碰到坚果
        self.isMeetWallNut = False
        # 僵尸是否活着
        self.isAlive = True

    def update(self, *args):
        if self.energy>0:
            if self.isMeetWallNut:
                # 显示吃坚果动画
                self.image = self.attackimages[args[0] % len(self.attackimages)]
            else:
                # 更新图片
                self.image = self.images[args[0] % len(self.images)]

            # 更新位置
            if self.rect.left > 250 and not self.isMeetWallNut:
                self.rect.left -= self.speed
        else:
            if self.dietimes < 20:
                self.image = self.dieimages[self.dietimes//2]
                self.dietimes += 1
            else:
                if self.dietimes > 30:
                    # 改变僵尸的活的状态
                    self.isAlive = False
                    # 销毁
                    self.kill()
                else:
                    self.dietimes += 1